/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zpsb.accounting.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
//import javax.persistence.Query;
import javax.persistence.TypedQuery;
import zpsb.accounting.model.User;

/**
 *
 * @author Michal
 */
@Stateless
public class UserBean {

    @PersistenceContext
    EntityManager em;
    
    public void create(User user) {
        em.persist(user);
    }
    
    public List<User> getAllUsers() {
        TypedQuery<User> q = em.createQuery("select u from User u", User.class);
        
//        Query q1 = em.createQuery("select u from User u");
//        List list = q1.getResultList();
//        return list;
        return q.getResultList();
    }
    
    public boolean authenticate(String username, String password) {
        TypedQuery<User> q = em.createQuery("select u from User u where u.userName = :a1 "
                + "and u.password = :password", User.class);
        q.setParameter("a1", username);
        q.setParameter("password", password);
        
        if (q.getSingleResult() == null) {
            return false;
        } else {
            return true;
        }
    }

}
